package com.qdlc.p2p.biz.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.BaseGiveAwardService;
import com.qdlc.p2p.common.enums.EnumActicityPlanNid;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.dal.dao.NewActivityAwardLogDao;
import com.qdlc.p2p.dal.dao.RedEnvelopeDao;
import com.qdlc.p2p.dal.dao.UserRedEnvelopeDao;
import com.qdlc.p2p.dal.dto.NewActivityAwardLog;
import com.qdlc.p2p.dal.dto.RedEnvelope;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserRedEnvelope;
import com.qdlc.p2p.dal.model.BaseGiveAwardModel;
import com.qdlc.p2p.dal.model.RedEnvelopeModel;

/**
 * Created by liaoxiangkai on 2015/11/17.
 */
@Service("giveUserRedEnvelopeService")
public class GiveUserRedEnvelopeServiceImpl implements BaseGiveAwardService {
    private static Logger logger = Logger.getLogger(GiveUserRedEnvelopeServiceImpl.class);

    @Resource
    private UserRedEnvelopeDao userRedEnvelopeDao;

    @Resource
    private RedEnvelopeDao redEnvelopeDao;

    @Resource
    private NewActivityAwardLogDao newActivityAwardLogDao;

    @SuppressWarnings("static-access")
	@Override
    public void giveAward(BaseGiveAwardModel baseGiveAwardModel) {
        double money = baseGiveAwardModel.getAmount();

        //获取用户
        User user = baseGiveAwardModel.getUser();
        if (null == user) {
            return;
        }
        //获取红包方案
        RedEnvelope redEnvelope = redEnvelopeDao.find(baseGiveAwardModel.getRuleId());
        if (null == redEnvelope) {
            logger.info("红包方案不存在，红包ID:" + baseGiveAwardModel.getRuleId());
            return;
        }
        RedEnvelopeModel model = RedEnvelopeModel.instance(redEnvelope);

        // 检测红包常规属性
        boolean checkResult = model.checkUseModel();
        if (!checkResult) {
            return;
        }

        /**
         * 发放红包：
         *  if是非固定金额的红包，
         *  else为固定红包
         */

        if (money != 0) {
            UserRedEnvelopeServiceImpl userRedEnvelopeServiceImpl = (UserRedEnvelopeServiceImpl) BeanUtil.getBean("userRedEnvelopeService");
            // 计算红包发放金额
            double amount = userRedEnvelopeServiceImpl.calculationAmount(money, model);

            // 如果红包金额大于0则发放红包
            if (amount > 0) {
                UserRedEnvelope userRedEnvelope = new UserRedEnvelope(user,
                        model.getName(), amount, model.getType(),
                        model.getValidDay(), baseGiveAwardModel.getSourceType());

                // 更新红包已发放数量
                redEnvelope.setLssueNum(redEnvelope.getLssueNum() + 1);
                redEnvelopeDao.update(redEnvelope);
                userRedEnvelopeDao.save(userRedEnvelope);
            }
        } else {
            // 如果发放方式不是固定金额则返回null
            if (model.RED_ENVELOPE_PAY_FIXED_AMOUNT != model.getPayType()) {
                return;
            }
            // 如果红包金额大于0则发放红包
            if (model.getAmount() > 0) {
                UserRedEnvelope userRedEnvelopen = new UserRedEnvelope(user,
                        model.getName(), model.getAmount(), model.getType(),
                        model.getValidDay(), baseGiveAwardModel.getSourceType());

                // 更新红包已发放数量
                redEnvelope.setLssueNum(redEnvelope.getLssueNum() + 1);
                redEnvelopeDao.update(redEnvelope);
                userRedEnvelopen = userRedEnvelopeDao.save(userRedEnvelopen);
                if (userRedEnvelopen != null) {
                    //赠与活动记录
                    NewActivityAwardLog newActivityAwardLog = new NewActivityAwardLog();
                    newActivityAwardLog.setUser(user);
                    newActivityAwardLog.setTache(baseGiveAwardModel.getSourceType());
                    newActivityAwardLog.setRuleType(baseGiveAwardModel.getRuleType());
                    newActivityAwardLog.setRuleId(baseGiveAwardModel.getRuleId());
                    newActivityAwardLog.setCount(1);
                    newActivityAwardLog.setAmount(model.getAmount());
                    newActivityAwardLog.setAddTime(new Date());
                    newActivityAwardLogDao.save(newActivityAwardLog);
                }
            }
        }
    }

    /**
     * 发放给单个用户
     * @param baseGiveAwardModel
     */
    @Override
    public void giveSingleUserAward(BaseGiveAwardModel baseGiveAwardModel) {
        UserRedEnvelope userRedEnvelope = new UserRedEnvelope(baseGiveAwardModel.getUser(),
                baseGiveAwardModel.getDescription(), baseGiveAwardModel.getAmount(), (byte)baseGiveAwardModel.getRuleType(),
                baseGiveAwardModel.getValidDay(), EnumActicityPlanNid.LOTTERY.getValue());
        userRedEnvelope =  userRedEnvelopeDao.save(userRedEnvelope);
        if (null != userRedEnvelope) {
            NewActivityAwardLog newActivityAwardLog = new NewActivityAwardLog(baseGiveAwardModel.getUser(), baseGiveAwardModel.getSourceType(), baseGiveAwardModel.getAmount(), BaseGiveAwardModel.RULE_TYPE_REDENVELOPE);
            newActivityAwardLogDao.save(newActivityAwardLog);
        }
    }
}
